package liujinliang.natappvip.cc.ccall;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import liujinliang.natappvip.cc.ccall.model.Contact;
import liujinliang.natappvip.cc.ccall.ui.AddressFragment;
import liujinliang.natappvip.cc.ccall.ui.CallFragment;
import liujinliang.natappvip.cc.ccall.ui.FriendFragment;
import liujinliang.natappvip.cc.ccall.ui.SettingsFragment;

/**
 * 第一条主线：ImageResource 从 normal 状态 到 pressed 状态
 * 第二条主线：Fragment 从 隐藏（如果为null，需要先创建） 到 显示
 * 事件的触发：交给 底部导航栏 四个控件:call, friend, address, settings
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    /**
     * 底部导航控件，用来触发事件
     */
    private LinearLayout callTab;
    private LinearLayout friendTab;
    private LinearLayout addressTab;
    private LinearLayout settingsTab;

    /**
     * 底部导航控件图片，用来表征事件发生在谁身上
     */
    private ImageButton callBtn;
    private ImageButton friendBtn;
    private ImageButton addressBtn;
    private ImageButton settingsBtn;

    /**
     * Fragment，用于展示当前被点击的控件
     */
    private Fragment callFragment;
    private Fragment friendFragment;
    private Fragment addressFragment;
    private Fragment settingsFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 隐藏 ActionBar
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }

        initViews();
        initEvents();
        // 初始页面
        setSelect(0);
        // 判断权限
        isPermissions();
    }

    /**
     * 判断用户是否授权
     */
    private void isPermissions() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {

            // 如果用户没有授权，就向用户申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 1);

        } else {

        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            // 如果用户没有授权，就向用户申请权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 2);

        } else {

        }
    }

    /**
     * 初始化控件
     */
    public void initViews() {
        // 初始化 底部导航栏的 四个控件，之后会添加事件，控制Fragment的切换
        callTab = (LinearLayout) findViewById(R.id.call_tab);
        friendTab = (LinearLayout) findViewById(R.id.friend_tab);
        addressTab = (LinearLayout) findViewById(R.id.address_tab);
        settingsTab = (LinearLayout) findViewById(R.id.settings_tab);

        // 初始化 底部导航栏的 四个控件 的 图片，后面需要对他们进行图片切换，来提示用户当前的点击状态
        callBtn = (ImageButton) findViewById(R.id.call_img_btn);
        friendBtn = (ImageButton) findViewById(R.id.friend_img_btn);
        addressBtn = (ImageButton) findViewById(R.id.address_img_btn);
        settingsBtn = (ImageButton) findViewById(R.id.settings_img_btn);
    }

    /**
     * 为底部导航添加点击事件
     */
    public void initEvents() {
        // 为底部导航添加点击事件
        callTab.setOnClickListener(this);
        friendTab.setOnClickListener(this);
        addressTab.setOnClickListener(this);
        settingsTab.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        initImages();

        switch (v.getId()) {
            case R.id.call_tab:
                setSelect(0);
                break;
            case R.id.friend_tab:
                setSelect(1);
                break;
            case R.id.address_tab:
                setSelect(2);
                break;
            case R.id.settings_tab:
                setSelect(3);
                break;
            default:
                break;
        }
    }

    public void setSelect(int select) {
        // 初始化 FragmentManager
        FragmentManager manager = getSupportFragmentManager();

        // 初始化 FragmentTransaction
        FragmentTransaction transaction = manager.beginTransaction();

        // 隐藏所有的 Fragment
        initFragment(transaction);

        switch (select) {
            case 0:
                // 如果 Fragment 没有被初始化，就初始化并把它添加到事务中
                if (callFragment == null) {
                    callFragment = new CallFragment();
                    transaction.add(R.id.fragment_content, callFragment);
                } else {
                    transaction.show(callFragment);
                }
                callBtn.setImageResource(R.drawable.tab_call_pressed);
                break;
            case 1:
                // 如果 Fragment 没有被初始化，就初始化并把它添加到事务中
                if (friendFragment == null) {
                    friendFragment = new FriendFragment();
                    transaction.add(R.id.fragment_content, friendFragment);
                } else {
                    transaction.show(friendFragment);
                }
                friendBtn.setImageResource(R.drawable.tab_friend_pressed);
                break;
            case 2:
                // 如果 Fragment 没有被初始化，就初始化并把它添加到事务中
                if (addressFragment == null) {
                    addressFragment = new AddressFragment();
                    transaction.add(R.id.fragment_content, addressFragment);
                } else {
                    transaction.show(addressFragment);
                }
                addressBtn.setImageResource(R.drawable.tab_address_pressed);
                break;
            case 3:
                // 如果 Fragment 没有被初始化，就初始化并把它添加到事务中
                if (settingsFragment == null) {
                    settingsFragment = new SettingsFragment();
                    transaction.add(R.id.fragment_content, settingsFragment);
                } else {
                    transaction.show(settingsFragment);
                }
                settingsBtn.setImageResource(R.drawable.tab_settings_pressed);
                break;
            default:
                break;
        }

        transaction.commit();
    }

    private void initFragment(FragmentTransaction transaction) {
        if (callFragment != null) {
            transaction.hide(callFragment);
        }

        if (friendFragment != null) {
            transaction.hide(friendFragment);
        }

        if (addressFragment != null) {
            transaction.hide(addressFragment);
        }

        if (settingsFragment != null) {
            transaction.hide(settingsFragment);
        }
    }

    private void initImages() {

        // 将所有图片初始化为没有被点击的状态
        callBtn.setImageResource(R.drawable.tab_call_normal);
        friendBtn.setImageResource(R.drawable.tab_friend_normal);
        addressBtn.setImageResource(R.drawable.tab_address_normal);
        settingsBtn.setImageResource(R.drawable.tab_settings_normal);
    }

    /**
     * 用户申请权限的回调函数
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    Toast.makeText(this, "You denied the 1 permission", Toast.LENGTH_SHORT).show();
                }
                break;
            case 2:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                } else {
                    Toast.makeText(this, "You denied the 2 permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

}
